package us.nchu.tomtiger.dao;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import us.nchu.tomtiger.model.User;
import us.nchu.tomtiger.rowMapper.UserRowMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/8.
 */
@Repository
public class UserDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    public boolean insert(User user) {
        String sql = "insert into user(phone_number, pass_word, nick_name, user_type, school_name, major, contact_type, contact, intro, certificate, student_id) values(?,?,?,?,?,?,?,?,?,?,?);";

        Object[] objects = new Object[] {
                user.getPhoneNumber(),
                user.getPassword(),
                user.getNickName(),
                user.getUserType(),
                user.getSchoolName(),
                user.getMajor(),
                user.getContact_type(),
                user.getContact(),
                user.getIntro(),
                user.getCertificate(),
                user.getStudentId()
        };

        return jdbcTemplate.update(sql, objects) > 0;
    }

    public void delete(String phoneNumber) {
        System.out.println("delete " + phoneNumber);
        String sql = "update user set status=0 where phone_number=?";
        Object[] objects = new Object[]{phoneNumber};
        jdbcTemplate.update(sql, objects);
    }

    public User query(String phoneNumber) {
        User user;

        String sql = "select * from user where phone_number=" + phoneNumber + ";";
        user = (User) jdbcTemplate.queryForObject(sql, new UserRowMapper());

        return user;
    }

    public List<User> queryAll() {
        String sql = "select * from user;";

        return jdbcTemplate.query(sql, new UserRowMapper());
    }


    public boolean update(User user) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("update user set");

        stringBuilder.append(" nick_name='" + user.getNickName() + "'");
//        stringBuilder.append(",user_type=" + user.getUserType());
        stringBuilder.append(",school_name='" + user.getSchoolName() + "'");
        stringBuilder.append(",major='" + user.getMajor() + "'");
        stringBuilder.append(",contact_type=" + user.getContact_type());
        stringBuilder.append(",contact='" + user.getContact() + "'");
        stringBuilder.append(",intro='" + user.getIntro() + "'");

        stringBuilder.append(" where phone_number=" + user.getPhoneNumber() + ";");

        return jdbcTemplate.update(stringBuilder.toString()) > 0;
    }

    public boolean updateAdmin(User user) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("update user set");

        stringBuilder.append(" nick_name='" + user.getNickName() + "'");
        stringBuilder.append(",user_type=" + user.getUserType());
        stringBuilder.append(",school_name='" + user.getSchoolName() + "'");
        stringBuilder.append(",major='" + user.getMajor() + "'");
        stringBuilder.append(",contact_type=" + user.getContact_type());
        stringBuilder.append(",contact='" + user.getContact() + "'");
        stringBuilder.append(",intro='" + user.getIntro() + "'");

        stringBuilder.append(" where phone_number=" + user.getPhoneNumber() + ";");

        return jdbcTemplate.update(stringBuilder.toString()) > 0;
    }

    public boolean updateUserType(String phoneNumber, int userType) {
        String sql = "update user set user_type=? where phone_number=" + phoneNumber + ";";
        Object[] objects = new Object[]{userType};

        return jdbcTemplate.update(sql, objects) > 0;
    }
}
